import 'dart:async';

import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lcapp/api/interface/user_api.dart';
import 'package:lcapp/api/request/response_data.dart';
import 'package:lcapp/data/lc_video_data_entity.dart';
import 'package:lcapp/model/event/event_bus.dart';
import 'package:lcapp/model/refresh/refresh.dart';
import 'package:lcapp/page/min/min_video_item.dart';

class MinVideoPage extends StatefulWidget {
  final bool isMine;
  final int sTyep; //1 作品 2 意向
  final int uid;
  const MinVideoPage({Key? key, required this.sTyep, required this.isMine, required this.uid}) : super(key: key);

  @override
  _MinVideoPageState createState() => _MinVideoPageState();
}

class _MinVideoPageState extends State<MinVideoPage> {

  int pageNum = 1;
  int pageSize = 20;
  List<LcVideoDataEntity> lcVideos = [];
  late StreamSubscription _streamSubscription;
  bool isDone = true;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _streamSubscription = eventBus.on<FabuState>().listen((event) {
      if (event.type == 0) {
        pageNum=1;
        getVideos();
      }
    });
    getVideos();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _streamSubscription.cancel();
  }

  getVideos()async{
    if(isDone==false){
      return;
    }
    isDone = false;
    ResponseData res;
    if(widget.sTyep==1){
      res = await UserAPI.getMinVideos(widget.uid, pageNum, pageSize);
    }else{
      res = await UserAPI.getMinLikces(widget.uid, pageNum, pageSize);
    }
    isDone = true;

    if(res.code==200){
      if(pageNum==1){
        lcVideos.clear();
      }
      List arr = res.data??[];
      for(var e in arr){
        LcVideoDataEntity entity = LcVideoDataEntity().fromJson(e);
        if(widget.sTyep==2){
          entity.likeState=1;
        }
        lcVideos.add(entity);
      }

    }
    if(mounted){setState(() {
    });}
  }


  @override
  Widget build(BuildContext context) {
    return EasyRefresh(
      header: myRefreshHeader(),
      footer: myRefreshFooter(),
      onRefresh: ()async{
        pageNum=1;
        getVideos();
      },
      onLoad: ()async{
        pageNum++;
        getVideos();
      },
      child: GridView.builder(
          padding: EdgeInsets.zero,
          gridDelegate:
           SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 3,
              mainAxisSpacing: 4.w,
              crossAxisSpacing: 4.w,
              childAspectRatio: 0.8
          ),
          shrinkWrap: true,
          itemCount: lcVideos.length,
          itemBuilder: (BuildContext context, int i) {
            return MinVideoItem(entity: lcVideos[i],clickFun: (){
                Navigator.of(context).pushNamed("MinVideoDetailPage",arguments: {
                   'lcVideos':lcVideos,
                  'isMine':widget.isMine&&widget.sTyep==1,
                  'index':i
                }).then((value){
                  if(widget.sTyep==2){
                    for(int i=0;i<lcVideos.length;i++){
                      print('lc======' + lcVideos[i].likeState.toString());
                      if(lcVideos[i].likeState==null||lcVideos[i].likeState==0){
                        setState(() {
                          lcVideos.removeAt(i);
                        });
                      }
                    }
                  }
                });
            },);
          }),
    );
  }
}
